﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using AvalonDock;
using QLab.Utility;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using System.Windows.Input;
using QLab.Services;
using Microsoft.Practices.Prism.Commands;

namespace QLab.Controllers
{
	/// <summary>
	/// Klasa koja se bavi organizacijom tabova (dodavanjem, brisanjem/gašenjem, da li je sadržaj sačuvan, ...)
	/// Tabovi koji se dodaju van ove klase se ignorišu (npr. ignoriše tab sa konzolom kad ga korisnik prevuče)
	/// </summary>
	[Export]
	[PartCreationPolicy(CreationPolicy.Shared)]
	public class TabController : NotificationObject
	{
		private DocumentPane _view;
		private ObservableCollection<TabBase> _tabs;
		private IRegionManager _regionManager;
		private IStatusBarService _statusBarService;

		private ICommand _currentTabSaveCommand;
		private ICommand _saveAllCommand;
		private ICommand _cutCommand;
		private ICommand _copyCommand;
		private ICommand _pasteCommand;
		private ICommand _undoCommand;
		private ICommand _redoCommand;

		[ImportingConstructor]
		public TabController(IRegionManager RegionManager, IStatusBarService StatusBarService)
		{
			_regionManager = RegionManager;
			_statusBarService = StatusBarService;
			_tabs = new ObservableCollection<TabBase>();

			_saveAllCommand = new DelegateCommand(SaveAll);
		}

		#region Properties

		public ObservableCollection<TabBase> Tabs { get { return _tabs; } }

		public DocumentPane DocumentPane 
		{
			get { return _view;}
			set 
			{
				if (_view != value)
				{
					_view = value;
					if (_view != null)
					{
						_view.SelectionChanged += ViewSelectionChanged;
					}
					RaisePropertyChanged(() => DocumentPane);					
				}
			}
		}

		public ICommand CurrentTabSaveCommand
		{
			get { return _currentTabSaveCommand; }
			set
			{
				_currentTabSaveCommand = value;
				RaisePropertyChanged(() => CurrentTabSaveCommand);
			}
		}

		public ICommand SaveAllCommand
		{
			get { return _saveAllCommand; }
			set
			{
				_saveAllCommand = value;
				RaisePropertyChanged(() => SaveAllCommand);
			}
		}

		public ICommand CutCommand
		{
			get { return _cutCommand; }
			set
			{
				_cutCommand = value;
				RaisePropertyChanged(() => CutCommand);
			}
		}

		public ICommand CopyCommand
		{
			get { return _copyCommand; }
			set
			{
				_copyCommand = value;
				RaisePropertyChanged(() => CopyCommand);
			}
		}

		public ICommand PasteCommand
		{
			get { return _pasteCommand; }
			set
			{
				_pasteCommand = value;
				RaisePropertyChanged(() => PasteCommand);
			}
		}

		public ICommand UndoCommand
		{
			get { return _undoCommand; }
			set
			{
				_undoCommand = value;
				RaisePropertyChanged(() => UndoCommand);
			}
		}

		public ICommand RedoCommand
		{
			get { return _redoCommand; }
			set
			{
				_redoCommand = value;
				RaisePropertyChanged(() => RedoCommand);
			}
		}

		#endregion

		#region Methods	

		public void AddTab(TabBase Tab)
		{
			foreach (TabBase tb in _tabs)
				if (tb.EqualTo(Tab))
				{
					_view.SelectedItem = tb;
					return;
				}

			Tab.Closed += TabClosed;
			Tab.StatusBarService = _statusBarService; //Inject status bar service

			_tabs.Add(Tab);
			_view.Items.Add(Tab);
			_view.SelectedItem = Tab;
		}

		void TabClosed(object sender, EventArgs e)
		{
			RemoveTab(sender as TabBase);
		}

		internal void RemoveTab(TabBase Tab)
		{
			_tabs.Remove(Tab);
		}

		internal void SaveAll()
		{
			if (_tabs.Count > 0)
			{
				foreach (TabBase t in _tabs)
					t.SaveCommand.Execute(null);
				_statusBarService.SetStatus("File(s) saved successfully");
			}
			else
			{
				_statusBarService.SetStatus("Nothing to save.");
			}
		}

		#endregion

		#region Event Handlers

		void ViewSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
			if (e.AddedItems.Count > 0)
			{
				TabBase tab = e.AddedItems[0] as TabBase;
				if (tab != null)
				{
					CurrentTabSaveCommand = tab.SaveCommand;
					CutCommand = tab.CutCommand;
					CopyCommand = tab.CopyCommand;
					PasteCommand = tab.PasteCommand;
					UndoCommand = tab.UndoCommand;
					RedoCommand = tab.RedoCommand;
					return;
				}
			}
			CurrentTabSaveCommand = null;
			CutCommand = null;
			CopyCommand = null;
			PasteCommand = null;
			UndoCommand = null;
			RedoCommand = null;
		} 

		#endregion

	}
}
